﻿

using System;
using System.Collections.Generic;
using System.Data;
using BusinessObjects.BussinessDictionary;
using DataObjects.Shared;
using DataObjects.StandartInterfaces.Dictionary;

namespace DataObjects.ADO.SqlServer.Dictionary
{

    /// <summary>
    /// 
    /// </summary>
    public class SqlServerDepartment : IDepartmentDao 
    {
        /// <summary>
        /// Takes the specified employ test.
        /// </summary>
        /// <param name="department">The employ test.</param>
        /// <returns></returns>
        private static object[] Take(Department department)
        {
            return new object[]  
            {
               "@DepartmentID", department.DepartmentId,
                "@Code", department.Code,
                "@Description", department.Description,
                "@Address", department.Address,
                "@PhoneNumber", department.PhoneNumber,
                "@ParentId", department.ParentId,
                "@UnitId", department.UnitId,
                "@Name", department.Name,
                "@Fax", department.Fax,
                "@IsActive", department.IsActive
            };
        }

        /// <summary>
        /// Creates query parameters list from Department object
        /// </summary>
        /// <param name="departmentId"></param>
        /// <returns>Name value parameter list.</returns>
        private static object[] Take(int departmentId, long unitId)
        {
            return new object[]  
            {
                "@DepartmentID", departmentId,
                "@UnitID", unitId
            };
        }
        /// <summary>
        /// The make
        /// </summary>
        private static readonly Func<IDataReader, Department> Make = reader =>
          new Department
          {
              DepartmentId = reader["DepartmentId"].AsId(),
              Code = reader["Code"].AsString(),
              Description = reader["Description"].AsString(),
              Address = reader["Address"].AsString(),
              PhoneNumber = reader["PhoneNumber"].AsString(),
              ParentId = reader["ParentId"].AsString().AsInteger(),
              UnitId = reader["UnitID"].AsInt(),
              Name = reader["Name"].AsString(),
              Fax = reader["Fax"].AsString(),
              IsActive = reader["IsActive"].AsBool()
          };

        /// <summary>
        /// Creates a Department object based on DataReader.
        /// </summary>
        private static readonly Func<IDataReader, Department> Make1 = reader =>
           new Department
           {
               DepartmentId = reader["DepartmentId"].AsId(),
               Code = reader["Code"].AsString(),
               Description = reader["Description"].AsString(),
               Address = reader["Address"].AsString(),
               PhoneNumber = reader["PhoneNumber"].AsString(),
               ParentId = reader["ParentId"].AsString().AsInteger(),
               UnitId = reader["UnitID"].AsInt(),
               Name = reader["Name"].AsString(),
               Fax = reader["Fax"].AsString(),
               IsActive = reader["IsActive"].AsBool(),
           //    HasChild = reader["HasChild"].AsBool()
           };

        /// <summary>
        /// Gets the paged.
        /// </summary>
        /// <param name="unitId">The unit identifier.</param>
        /// <param name="currentPage">The current page.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public List<Department> GetPaged(int unitId, int currentPage)
        {
            const string sql = @"uspGetPaged_Department";
            object[] parms = { "@WhereClause", null, "@OrderBy", null, "@PageIndex", currentPage - 1, "@UnitID", unitId };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }

        /// <summary>
        /// </summary>
        /// <param name="unitId"></param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public int GetPageSize(int unitId)
        {
            const string sql = @"uspGetPageSize_Department";
            object[] parms = { "@WhereClause", null, "@UnitID", unitId };
            return Db.GetCount(sql, CommandType.StoredProcedure, parms);
        }

       

       

        /// <summary>
        /// Inserts the department.
        /// </summary>
        /// <param name="department">The department.</param>
        /// <returns></returns>
        public string InsertDepartment(Department department)
        {
            const string sql = "uspInsert_Department";
            return Db.Insert(sql, CommandType.StoredProcedure, Take(department));
        }

        /// <summary>
        /// Updates the department.
        /// </summary>
        /// <param name="department">The department.</param>
        /// <returns></returns>
        public string UpdateDepartment(Department department)
        {
            const string sql = "uspUpdate_Department";
            return Db.Update(sql, CommandType.StoredProcedure, Take(department));
        }

        /// <summary>
        /// Deletes the department.
        /// </summary>
        /// <param name="department">The department.</param>
        /// <returns></returns>
        public string DeleteDepartment(Department department)
        {
            const string sql = @"uspDelete_Department";
            object[] parms = { "@DepartmentID", department.DepartmentId ,
                             "@UnitID", department.UnitId };
            return Db.Delete(sql, CommandType.StoredProcedure, parms);
        }





        public Department GetDepartment(int departmentId)
        {
            const string sql = @"uspGet_Department_ByID";
            object[] parms = { "@DepartmentID", departmentId };
            return Db.Read(sql, CommandType.StoredProcedure,Make, parms);
        }


        public List<Department> GetDepartmentByUnit(int unitId)
        {
            const string sql = @"uspGet_Department_ByUnitID";
            object[] parms = { "@UnitID", unitId };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }
    }
}
